package com.example.hotelrear.controller;

import com.example.hotelrear.entity.ResponseResult;
import com.example.hotelrear.util.OssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.UUID;

@RestController
@RequestMapping("/admin/common")
@Api(tags = "通用接口")
@Slf4j
public class OssUploadController {

    @Resource
    private OssUtil ossUtil;

    @PostMapping("/upload")
    @ApiOperation("文件上传")
    public ResponseResult<String> upload(MultipartFile file){
        log.info("文件上传：{}",file);
<<<<<<< HEAD

=======
>>>>>>> c858df565d55deb60e87bd48c878762d1fd5080a
        try {
            //原始文件名
            String originalFilename = file.getOriginalFilename();
            //截取原始文件名的后缀  ****.png
            String extension = originalFilename.substring(originalFilename.lastIndexOf("."));
            //构造新文件名称
            String objectName = UUID.randomUUID().toString()+ extension;

            String filePath = ossUtil.upload(file.getBytes(), objectName);
            return ResponseResult.success(filePath);

        } catch (IOException e) {
            log.error("文件上传失败",e);
        }

        return ResponseResult.error("文件上传失败");
    }
}

